define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/order/index',
                    multi_url: 'merchant/order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");
            table.on('click', '.user', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });
            table.on('click', '.spread', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });
            table.on('click', '.said', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/snapshot_address/info/id/' + id, '操作', {});
            });
            table.on('click', '.sid', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/info/id/' + id, '操作', {});
            });

            table.on('click', '.profit_sharing', function(re) {
                let id = $(this).data('id');
                $.post('merchant/order/profit_sharing', {
                    id: id
                }, function(re) {
                    layer.msg(re.msg);
                    if(re.code) {
                        table.bootstrapTable('refresh');
                    }  
                });
            });
            table.on('click', '.profit_sharing_qiangzhi', function(re) {
                let id = $(this).data('id');
                $.post('merchant/order/profit_sharing', {
                    id: id,
                    qiangzhi: true
                }, function(re) {
                    layer.msg(re.msg);
                    if(re.code) {
                        table.bootstrapTable('refresh');
                    }  
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'O.id',
                sortName: 'O.id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'uid', title: '用户ID', formatter: function(value, row, index) {
                            return '<a class="user" href="javascript:" data-id="' + row.uid + '">' + row.uid + '</a>';
                        }}, 
                        {field: 'spreadid', title: '推广ID', formatter: function(value, row, index) {
                            if(row.spreadid > 0) {
                                return '<a class="spread" href="javascript:" data-id="' + row.spreadid + '">' + row.spreadid + '</a>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'said', title: '快照地址id', formatter: function(value, row, index) {
                            if(row.said > 0) {
                                return '<a class="said" href="javascript:" data-id="' + row.said + '">' + row.said + '</a>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'OL.tracking', title: '快递单号', formatter: function(value, row, index) {
                            return row.tracking;
                        }, operate: 'LIKE'},
                        {field: 'out_trade_no', title: '订单编号', operate: 'LIKE'},
                        {field: 'total_fee', title: '总金额'},
                        {field: 'commission', title: '佣金'},
                        {field: 'refund_fee', title: '退款金额'},
                        {field: 'spbill_create_ip', title: '用户IP'},
                        {field: 'profit_sharing', title: '是否分账', formatter: function(value, row, index) {
                            return row.profit_sharing_text;
                        }, searchList: window.profitSharingOptions},
                        {field: 'sid', title: '店铺ID', formatter: function(value, row, index) {
                            if(row.sid > 0) {
                                return '<a class="sid" href="javascript:" data-id="' + row.sid + '">' + row.sid + '</a>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'sub_mch_id', title: '商户号', operate: 'LIKE'},
                        {field: 'status', title: '订单状态', formatter: function(value, row, index) {
                            return row.status_text;
                        }, searchList: window.statusOptions},
                        {field: 'paytime', title: '支付时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'delivery_time', title: '发货时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '修改时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: '', title: '开始分账', formatter: function(value, data, index) {
                            if(data.is_profit_sharing && !data.profit_sharing) {
                                return '<button id="profitSharing" class="btn btn-info profit_sharing" data-id="' + data.id + '">分账</button>';
                            }
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        one: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/order/one',
                    multi_url: 'merchant/order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");
            table.on('click', '.user', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });
            table.on('click', '.spread', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });
            table.on('click', '.said', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/snapshot_address/info/id/' + id, '操作', {});
            });
            table.on('click', '.sid', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/info/id/' + id, '操作', {});
            });

            table.on('click', '.profit_sharing', function(re) {
                let id = $(this).data('id');
                $.post('merchant/order/profit_sharing', {
                    id: id
                }, function(re) {
                    layer.msg(re.msg);
                    if(re.code) {
                        table.bootstrapTable('refresh');
                    }  
                });
            });
            table.on('click', '.profit_sharing_qiangzhi', function(re) {
                let id = $(this).data('id');
                $.post('merchant/order/profit_sharing', {
                    id: id,
                    qiangzhi: true
                }, function(re) {
                    layer.msg(re.msg);
                    if(re.code) {
                        table.bootstrapTable('refresh');
                    }  
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'O.id',
                sortName: 'O.id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 0,

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'uid', title: '用户ID', formatter: function(value, row, index) {
                            return '<a class="user" href="javascript:" data-id="' + row.uid + '">' + row.uid + '</a>';
                        }}, 
                        {field: 'spreadid', title: '推广ID', formatter: function(value, row, index) {
                            if(row.spreadid > 0) {
                                return '<a class="spread" href="javascript:" data-id="' + row.spreadid + '">' + row.spreadid + '</a>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'said', title: '快照地址id', formatter: function(value, row, index) {
                            if(row.said > 0) {
                                return '<a class="said" href="javascript:" data-id="' + row.said + '">' + row.said + '</a>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'OL.tracking', title: '快递单号', formatter: function(value, row, index) {
                            return row.tracking;
                        }, operate: 'LIKE'},
                        {field: 'out_trade_no', title: '订单编号', operate: 'LIKE'},
                        {field: 'total_fee', title: '总金额'},
                        {field: 'commission', title: '佣金'},
                        {field: 'refund_fee', title: '退款金额'},
                        {field: 'spbill_create_ip', title: '用户IP'},
                        {field: 'profit_sharing', title: '是否分账', formatter: function(value, row, index) {
                            return row.profit_sharing_text;
                        }, searchList: window.profitSharingOptions},
                        {field: 'sid', title: '店铺ID', formatter: function(value, row, index) {
                            if(row.sid > 0) {
                                return '<a class="sid" href="javascript:" data-id="' + row.sid + '">' + row.sid + '</a>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'sub_mch_id', title: '商户号', operate: 'LIKE'},
                        {field: 'status', title: '订单状态', formatter: function(value, row, index) {
                            return row.status_text;
                        }, searchList: window.statusOptions},
                        {field: 'paytime', title: '支付时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'delivery_time', title: '发货时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '修改时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        set_msg_jump_path: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});